package design.graduation.novelsystem.controller;

import design.graduation.novelsystem.model.domian.IncludeBook;
import design.graduation.novelsystem.model.dto.ResultObj;
import design.graduation.novelsystem.model.vo.IncludeBookVo;
import design.graduation.novelsystem.service.BookContentService;
import design.graduation.novelsystem.service.IncludeBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: 洪少尉
 * @Date: 2020/4/18 12:48
 * @Description: 收录小说信息管理
 */
@RestController
@RequestMapping("includeBook")
public class IncludeBookController {
	@Autowired
	private IncludeBookService includeBookService;
	@Autowired
	private BookContentService bookContentService;

	/**
	 * 全查询+条件查询
	 */
	@GetMapping("loadAllIncludeBook")
	public Object loadAllIncludeBook(IncludeBookVo includeBookVo){
		return this.includeBookService.queryAllIncludeBook(includeBookVo);
	}
	/**
	 * 修改上下架状态
	 */
	@PostMapping("updateStatus")
	public ResultObj updateStatus(IncludeBook includeBook){
		if(this.includeBookService.updateStatus(includeBook)){
			bookContentService.updateNovelStatus(includeBook.getId(),includeBook.getStatus());
			return ResultObj.UPDATE_SUCCESS;
		}
		return ResultObj.UPDATE_ERROR;
	}
	/**
	 * 批量修改上架
	 */
	@PostMapping("batchUp")
	public ResultObj batchUp(Integer[] ids) {
		if (null != ids && ids.length > 0) {
			List<Integer> idsList = new ArrayList<>(Arrays.asList(ids));
			for (Integer id : idsList) {
				this.includeBookService.batchUp(id);
				//暂时不用
				//bookContentService.updateNovelStatus(id, Constant.AVAILABLE_TRUE);
			}
			return ResultObj.BATCH_UP_SUCCESS;
		}
		return ResultObj.BATCH_UP_ERROR;
	}
	/**
	 * 批量修改下架
	 */
	@PostMapping("batchDown")
	public ResultObj batchDown(Integer[] ids) {
		if (null != ids && ids.length > 0) {
			List<Integer> idsList = new ArrayList<>(Arrays.asList(ids));
			for (Integer id : idsList) {
				this.includeBookService.batchDown(id);
				//暂时不用
				// bookContentService.updateNovelStatus(id, Constant.AVAILABLE_FALSE);
			}
			return ResultObj.BATCH_DOWN_SUCCESS;
		}
		return ResultObj.BATCH_DOWN_ERROR;
	}
}
